﻿package 
{
	import flash.display.*;
	import flash.events.*;
	import flash.text.*;
	import flash.net.*;
	import com.totallypracticalgames.unityflashbridge.UnityFlashIntegrator;
	//import nl.demonsters.debugger.MonsterDebugger;// debugging only
	
	/**
	 * ...
	 * @author Roger Clark
	 */
	 /*
	 * @Desctiption
	 * 
	 * Test application for use with the UnityFlashBridge 
	 * Gets list of variables that have been passed by Unity to the Launcher, displays them, allows the user to change them and sends the vars back 
	 * to the Launcher when the user has finished.
	 *
	 */
	public class ListVars extends Sprite 
	{
		private var varsArray:Array;
		public var buttonE:SimpleButton;
		public var Debug2:TextField;
		private var ufi:UnityFlashIntegrator = UnityFlashIntegrator.getInstance();// get UFI singleton instance which will contain the variables (if there are any)
	//	private var mdb:MonsterDebugger = new MonsterDebugger(this);// Instantiate the Monster debugger
		
		
		// Constructor
		public function ListVars():void 
		{
			if (stage) 
			{
				init();
			}
			else
			{
				addEventListener(Event.ADDED_TO_STAGE, init);
			}
		}

			
		// Default contstuctor
		private function init(e:Event = null):void 
		{
			removeEventListener(Event.ADDED_TO_STAGE, init);
			
			this.buttonE.addEventListener(MouseEvent.MOUSE_UP, buttonEHandler);
			//MonsterDebugger.trace(this,"init()");
	
			startHandler(); 
		}
		
	
		public function startHandler():void
		{
			var c:int=0;
			varsArray = [];// clear the current array

			// Iterate through the vars
			for(var i in ufi.unityVars)
			{

				var vDisp=new VariableDisplay();// Create a new item to show the var name and value.
				vDisp.varName.text=i;
				vDisp.varValue.text=ufi.unityVars[i];
				vDisp.x=200;
				vDisp.y=260+(c++*(vDisp.height+10));
				//MonsterDebugger.trace(this,"Vdisp "+i+" " + ufi.unityVars[i]);				
				varsArray[i]=vDisp;// keep record of the 
				this.addChild(vDisp);
			}
			this.dispatchEvent(new Event(UnityFlashIntegrator.CHILD_READY, true));
		}
			
		// Do something when the user presses the button	
		private function buttonEHandler(e:Event):void
		{			
			
			for(var i in varsArray)
			{
				ufi.unityVars[i]=varsArray[i].varValue.text;// Read the values from the textfields and put them into the unityVars in the UFI instance.
			}
			dispatchEvent(new Event(UnityFlashIntegrator.CHILD_EXIT,true));// Tell the Launcher that we've finished.
		}	
		
	}// Class
}//package